package com.jic.market.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.entity.CouponGoodsTemplate;
import com.jic.market.mapper.CouponGoodsTemplateMapper;
import com.jic.market.request.manager.coupon.template.CouponTemplateAddGoodsTriggerRequest;
import com.jic.market.request.manager.coupon.template.CouponTemplateDeleteGoodsRequest;
import com.jic.market.service.CouponGoodsTemplateService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 优惠券商品关联模版表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-03 01:44:17
 */
@Service
public class CouponGoodsTemplateServiceImpl implements CouponGoodsTemplateService {

    @Autowired
    private CouponGoodsTemplateMapper couponGoodsTemplateMapper;

    @Override
    public long insert(CouponGoodsTemplate couponGoodsTemplate) {
        long flag = couponGoodsTemplateMapper.insert(couponGoodsTemplate);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return couponGoodsTemplateMapper.delete(id);
    }

    @Override
    public long update(CouponGoodsTemplate couponGoodsTemplate) {
        return couponGoodsTemplateMapper.update(couponGoodsTemplate);
    }

    @Override
    public CouponGoodsTemplate queryByPrimaryKey(Long id) {
        return couponGoodsTemplateMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<CouponGoodsTemplate> query(CouponGoodsTemplate couponGoodsTemplate) {
        return couponGoodsTemplateMapper.query(couponGoodsTemplate);
    }

    @Override
    public PageResult<CouponGoodsTemplate> queryPage(CouponGoodsTemplate couponGoodsTemplate, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<CouponGoodsTemplate> list = couponGoodsTemplateMapper.queryPage(startIndex, pageQuery.getLimit(), couponGoodsTemplate);
        long count = couponGoodsTemplateMapper.queryPageCount(couponGoodsTemplate);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<Boolean> addCouponTemplateFromGoods(CouponTemplateAddGoodsTriggerRequest couponTemplateAddGoodsTriggerRequest) {
        if(CollectionUtils.isEmpty(couponTemplateAddGoodsTriggerRequest.getGoodsList())){
            return RestResult.success(true);
        }
        couponTemplateAddGoodsTriggerRequest.getGoodsList().stream().forEach(item->{
            CouponGoodsTemplate template=new CouponGoodsTemplate();
            template.setCreateTime(new Date());
            template.setCreator(couponTemplateAddGoodsTriggerRequest.getOperator());
            template.setCreatorName(couponTemplateAddGoodsTriggerRequest.getOperateName());
            template.setDeleteFlag(false);
//            template.setDeptId(getValue(couponTemplateAddGoodsTriggerRequest.getDeptId()));
            template.setGoodsId(item);
//            template.setMerchantId(getValue(couponTemplateAddGoodsTriggerRequest.getMerchantId()));
//            template.setStoreId(getValue(couponTemplateAddGoodsTriggerRequest.getStoreId()));
            template.setTemplateId(couponTemplateAddGoodsTriggerRequest.getTemplateId());
            template.setUpdateTime(template.getCreateTime());
            insert(template);
        });
        return RestResult.success(true);
    }

    public String getValue(Long param){
        if(param==null){
            return "";
        }
        return Long.toString(param);
    }

    @Override
    public RestResult<Boolean> deleteCouponTemplateGoods(CouponTemplateDeleteGoodsRequest couponTemplateDeleteGoodsRequest) {
        return null;
    }
}